package com.apes.fn.server.workDesk.smartHelper.process.inStore;

import com.alibaba.fastjson.JSONArray;
import com.apes.fn.server.workDesk.smartHelper.process.CommonSmartDataProcess;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: apes-server
 * @description:
 * @author: czs
 * @create: 2023-06-15 10:41
 **/
@Service
public class StockProcess extends CommonSmartDataProcess {

    private static final List<String> LEVEL_LIST = Arrays.asList("smartHelper","inStore","Warehouse","Stock");

    @Override
    public List<String> getLevelList() {
        return LEVEL_LIST;
    }

    @Override
    public boolean support(String type) {
        return true;
    }

    @Override
    public String getName() {
        return "库存";
    }

    @Override
    public Map findResultData(JSONObject resultMap) {
        JSONArray invoke = invoke("fn.workDeskConfig.stockInfo", resultMap);
        List<Object> objectList = invoke.stream().filter(o -> {
            Map jsonObject = (Map) o;
            try {
                jsonObject.put("ID",jsonObject.get("id"));
                jsonObject.put("NAME",jsonObject.get("name"));
                jsonObject.put("COUNT",jsonObject.get("count"));
                double count = (double) jsonObject.get("count");
                if (count == 0D) return false;
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }).collect(Collectors.toList());

        return mapper(
                "id",getLevelList().get(getLevelList().size()-1),
                "name",getName(),
                "data",new JSONArray(objectList)
        );
    }

    @Override
    public Integer findWeightValue() {
        return 40;
    }
}
